
const host =  '';
const ws_url="ws://"+window.location.host+":9201";

function count_click(only_query,call) {
    $.get(host+"/api/index/tag_count?tag=klee_click&is_only_query="+only_query,call);
}

function get_ranks(call) {
    $.get(host+"/api/index/rank?tag=klee_click",call);
}
function get_uid(call) {
    $.get(host+"/api/index/get_uid?tag=klee_click",call);
}
var ws ;
function  init_ws(uid,onmessage){
    ws  = new WebSocket(ws_url);
    // 获取连接状态
    console.log('ws连接状态：' + ws.readyState);
    //监听是否连接成功
    ws.onopen = function () {
        console.log('ws连接成功'+ws.readyState);
        //连接成功则发送一个数据
        console.log('发送消息：' + "@H:uid:"+uid+":klee_click");
        ws.send("@H:uid:"+uid+":klee_click");
    }
// 接听服务器发回的信息并处理展示
    ws.onmessage = function (data) {

        data= JSON.parse(data.data);
        if(data.data!==undefined){
            onmessage(data);
        }

        //完成通信后关闭WebSocket连接
        //ws.close();
    }
// 监听连接关闭事件
    ws.onclose = function () {
        // 监听整个过程中websocket的状态
        console.log('ws连接断开，尝试重连：' + ws.readyState);
        init_ws(uid,onmessage);
    };
// 监听并处理error事件
    ws.onerror = function (error) {
        console.log(error);
    }
    window.onbeforeunload = function() {
        ws.close();
    };
    document.addEventListener('visibilitychange',function() {
        if(document.visibilityState == 'hidden') {
            //记录页面隐藏时间
        } else {
                // 主动关闭连接
           if(ws.readyState!==1){
               init_ws(uid,onmessage);
           }
        }
    });
}

function send_click() {
    if(ws!==undefined){
        ws.send("@H:tag:klee_click");
        console.log('发送消息：' + "@H:tag:klee_click");
    }

}